# load packages -------------------------------------------------------------
pacman::p_load("car")

#additional functions
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

#load data ------------
load("DataverseData/B5_experiment.Rdata")


#Wave 1 ideology questions -----------------
merged$abortion <- car::recode(as.numeric(merged$Q3.8),"6=NA")
merged$gaymarriage <- car::recode(as.numeric(merged$Q3.10),"6=NA")
merged$immigrants <- 6-car::recode(as.numeric(merged$Q3.11),"6=NA")
merged$college <- car::recode(as.numeric(merged$Q3.12),"6=NA")

merged$pid <- NA
merged$pid[merged$Q3.2=='Strong Republican']=7
merged$pid[merged$Q3.2=='Not very strong Republican']=6
merged$pid[merged$Q3.3=='Strong Democrat']=1
merged$pid[merged$Q3.3=='Not very strong Democrat']=2
merged$pid[merged$Q3.4=='Closer to the Democratic Party']=3
merged$pid[merged$Q3.4=='Closer to the Republican Party']=5
merged$pid[merged$Q3.4=='Neither']=4
merged$approval <- car::recode(as.numeric(merged$Q3.13),"6=NA")
merged$libcon <- car::recode(as.numeric(merged$Q3.5),"8=NA")
merged$resp1 <- car::recode(as.numeric(merged$Q3.7),"6=NA")
merged$resp2 <- car::recode(as.numeric(merged$Q3.6),"6=NA")
merged$passed <- ifelse(merged$Q3.9_3=="Taxes" & merged$Q3.9_5=="None of the above",1,0)

#Create ideology dimensions-----------------
merged$general <- rowMeans(data.frame(zero1(merged$resp2),zero1(merged$resp1),zero1(merged$libcon),zero1(merged$pid),zero1(6-merged$approval),zero1(merged$gaymarriage),zero1(merged$abortion),zero1(merged$immigrants),zero1(merged$college)),na.rm=T)

merged$symbolic <- rowMeans(data.frame(zero1(merged$pid),zero1(merged$libcon),zero1(6-merged$approval)),na.rm=T)

merged$cult <- rowMeans(data.frame(zero1(merged$gaymarriage),zero1(merged$abortion),zero1(merged$immigrants),zero1(merged$college)),na.rm = T)

merged$econ <- rowMeans(data.frame(zero1(merged$resp2),zero1(merged$resp1)),na.rm=T)

#Passed or not---------
merged$passed[is.na(merged$passed)]=0

## Create Personality Traits
merged$openness <- zero1(rowMeans(data.frame(merged$bfiartistic,merged$bfiimagination,merged$bfioriginal)))
merged$conscientiousness <- zero1(rowMeans(data.frame(merged$bfic1,merged$bfic2,merged$bfic3)))
merged$extraversion <- zero1(rowMeans(data.frame(merged$bfie1,merged$bfie2,merged$bfie3)))
merged$agreeableness <- zero1(rowMeans(data.frame(merged$bfia1,merged$bfia2,merged$bfia3)))
merged$neuroticism <- zero1(rowMeans(data.frame(merged$bfin1,merged$bfin2,merged$bfin3)))

##Recode personality variables-------------
merged$opennessr <- 1-merged$openness
merged$agreeablenessr <- 1-merged$agreeableness

##Covariates-----------
merged$gender <- car::recode(as.numeric(merged$Q4.3),"1='Male';2='Female';3='Other';else=NA",as.factor=T)
merged$age <-(as.numeric(merged$Q4.6))
merged$education <-car::recode(as.numeric(merged$Q4.7),"1:2='HS or Less';3='Some College';4:5='College+'; 6=NA",as.factor = T)
merged$race <- 'Other'
merged$race[merged$Q4.5=='White']='White'
merged$race[merged$Q4.5=='Black or African American']='Black'
merged$race[merged$Q4.4=='Yes']='Hispanic'

#code pid and ideology 0-1------------------------
merged$pid01<-zero1(merged$pid)
merged$libcon01<-zero1(merged$libcon)
merged$approval01<-zero1(6-merged$approval)

#Save data
save(merged, file="Data/Experiment_merged.Rdata")
